﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using System.Web.Script.Serialization;

namespace SusuCMS
{
    public class Control
    {
        public int ControlType { get; set; }

        public string Title { get; set; }

        public string Name { get; set; }

        public string Data { get; set; }

        public int Index { get; set; }

        public string DefaultValue { get; set; }

        public string GetHtml()
        {
            var html = new StringBuilder();
            switch ((ControlType)this.ControlType)
            {
                case SusuCMS.ControlType.SingleLineText:
                    html.AppendFormat("<input type='text' name='{0}' id='{0}' value='{1}'/>", this.Name, this.DefaultValue);
                    break;
                case SusuCMS.ControlType.ParagraphText:
                    html.AppendFormat("<textarea name='{0}' id='{0}'>{1}</textarea>", this.Name, this.DefaultValue);
                    break;
                case SusuCMS.ControlType.MultipleChoice:
                    var multipleChoiceList = this.Data.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                    foreach (var value in multipleChoiceList)
                    {
                        html.AppendFormat("<label><input type='radio' name='{0}' {1} value='{2}' />{2}</label>", this.Name, this.DefaultValue == value ? "checked='checked'" : "", value);
                    }
                    break;
                case SusuCMS.ControlType.CheckBoxList:
                    var checkBoxList = this.Data.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                    var splitted = new [] {""};
                    if (!string.IsNullOrWhiteSpace(this.DefaultValue))
                    {
                        splitted = this.DefaultValue.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                    }
                    foreach (var value in checkBoxList)
                    {
                        html.AppendFormat("<label><input type='checkbox' name='{0}' {1} value='{2}' />{2}</label>", this.Name, splitted.Any(i => i == value) ? "checked='checked'" : "", value);
                    }
                    break;
                case SusuCMS.ControlType.DropDownList:
                    var dropDownList = this.Data.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                    html.AppendFormat("<select name='{0}' id='{0}' value='{1}'>", this.Name, this.DefaultValue);
                    foreach (var value in dropDownList)
                    {
                        html.AppendFormat("<option value='{0}' {1}>{0}</option>", value, this.DefaultValue == value ? "selected='selected'" : "", value);
                    }
                    html.Append("</select>");
                    break;
            }

            return html.ToString();
        }
    }
}
